<?php

use App\Http\Middleware\EncryptCookies;
use App\Http\Middleware\VerifyCsrfToken;
use Beike\Seller\Http\Controllers\AttributeController;
use Beike\Seller\Http\Controllers\BrandController;
use Beike\Seller\Http\Controllers\CategoryController;
use Beike\Seller\Http\Controllers\EditController;
use Beike\Seller\Http\Controllers\FileManagerController;
use Beike\Seller\Http\Controllers\HomeController;
use Beike\Seller\Http\Controllers\LanguageController;
use Beike\Seller\Http\Controllers\LoginController;
use Beike\Seller\Http\Controllers\LogoutController;
use Beike\Seller\Http\Controllers\OrderController;
use Beike\Seller\Http\Controllers\ProductController;
use Beike\Seller\Http\Controllers\RegisterController;
use Beike\Seller\Http\Controllers\ReportController;
use Beike\Seller\Http\Controllers\RmaController;
use Beike\Seller\Http\Controllers\ShippingController;
use Beike\Seller\Http\Controllers\TransactionController;
use Beike\Seller\Http\Controllers\UserController;
use Beike\Seller\Http\Controllers\WithdrawController;
use Beike\Seller\Middleware\SellerAuthenticate;
use Beike\Seller\Middleware\SetLocaleSeller;
use Beike\Seller\Models\SellerUser;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Route;
use Illuminate\View\Middleware\ShareErrorsFromSession;

Route::prefix('seller')
    ->name('seller.')
    ->middleware([EncryptCookies::class, StartSession::class, ShareErrorsFromSession::class, SetLocaleSeller::class, VerifyCsrfToken::class])
    ->group(function () {
        Route::get('register', [RegisterController::class, 'index'])->name('register.index');
        Route::post('register', [RegisterController::class, 'store'])->name('register.store');
        Route::get('login', [LoginController::class, 'index'])->name('login.index');
        Route::post('login', [LoginController::class, 'store'])->name('login.store');
        Route::get('logout', [LogoutController::class, 'index'])->name('logout');

        Route::middleware(SellerAuthenticate::class .':' . SellerUser::AUTH_GUARD)
            ->group(function () {
                Route::get('/', [HomeController::class, 'index'])->name('home.index');

                Route::get('lang', [LanguageController::class, 'index'])->name('lang.switch');

                Route::get('edit', [EditController::class, 'index'])->name('edit.index');
                Route::post('update', [EditController::class, 'update'])->name('edit.update');
                Route::get('password', [EditController::class, 'password'])->name('edit.password');
                Route::post('password', [EditController::class, 'savePassword'])->name('edit.save_password');

                Route::get('categories', [HomeController::class, 'index'])->name('categories.index');
                Route::get('orders', [HomeController::class, 'index'])->name('orders.index');
                Route::get('rmas', [HomeController::class, 'index'])->name('rmas.index');

                Route::post('attributes/{id}/values', [AttributeController::class, 'storeValue'])->name('attributes.values.store');
                Route::get('attributes/{id}/values/autocomplete', [AttributeController::class, 'autocompleteValue'])->name('attributes.values.autocomplete');
                Route::get('attributes/autocomplete', [AttributeController::class, 'autocomplete'])->name('attributes.autocomplete');
                Route::get('brands/autocomplete', [BrandController::class, 'autocomplete'])->name('brands.autocomplete');

                // 商户分类
                Route::middleware('can:categories_index')->get('categories', [CategoryController::class, 'index'])->name('categories.index');
                Route::middleware('can:categories_create')->get('categories/create', [CategoryController::class, 'create'])->name('categories.create');
                Route::middleware('can:categories_create')->post('categories', [CategoryController::class, 'store'])->name('categories.store');
                Route::middleware('can:categories_update')->get('categories/{id}/edit', [CategoryController::class, 'edit'])->name('categories.edit');
                Route::middleware('can:categories_update')->put('categories/{id}', [CategoryController::class, 'update'])->name('categories.update');
                Route::middleware('can:categories_delete')->delete('categories/{id}', [CategoryController::class, 'destroy'])->name('categories.destroy');

                // 商品
                Route::middleware('can:products_trashed')->put('products/restore', [ProductController::class, 'restore']);
                Route::middleware('can:products_trashed')->get('products/trashed', [ProductController::class, 'trashed'])->name('products.trashed');
                Route::middleware('can:products_trashed')->post('products/trashed/clear', [ProductController::class, 'trashedClear'])->name('products.trashed.clear');
                Route::middleware('can:products_index')->get('products/{id}/name', [ProductController::class, 'name'])->name('products.name');
                Route::middleware('can:products_index')->get('products/names', [ProductController::class, 'getNames'])->name('products.names');
                Route::middleware('can:products_index')->get('products/autocomplete', [ProductController::class, 'autocomplete'])->name('products.autocomplete');

                // 报表
                Route::middleware('can:reports_sale')->get('reports/sale', [ReportController::class, 'sale'])->name('reports_sale.index');
                Route::middleware('can:reports_view')->get('reports/view', [ReportController::class, 'view'])->name('reports_view.index');
                Route::middleware('can:reports_view')->get('reports/product_view/{product_id}', [ReportController::class, 'productView'])->name('reports_view.product');

                // 文件管理器
                Route::middleware('can:file_managers_read')->get('file_manager', [FileManagerController::class, 'index'])->name('file_manager.index');
                Route::middleware('can:file_managers_read')->get('file_manager/files', [FileManagerController::class, 'getFiles'])->name('file_manager.get_files');
                Route::middleware('can:file_managers_read')->get('file_manager/directories', [FileManagerController::class, 'getDirectories'])->name('file_manager.get_directories');
                Route::middleware('can:file_managers_read')->post('file_manager/directories', [FileManagerController::class, 'createDirectory'])->name('file_manager.create_directory');
                Route::middleware('can:file_managers_write')->post('file_manager/upload', [FileManagerController::class, 'uploadFiles'])->name('file_manager.upload');
                Route::middleware('can:file_managers_write')->post('file_manager/rename', [FileManagerController::class, 'rename'])->name('file_manager.rename');
                Route::middleware('can:file_managers_write')->delete('file_manager/files', [FileManagerController::class, 'destroyFiles'])->name('file_manager.delete_files');
                Route::middleware('can:file_managers_write')->delete('file_manager/directories', [FileManagerController::class, 'destroyDirectories'])->name('file_manager.delete_directories');
                Route::middleware('can:file_manager_update')->post('file_manager/move_directories', [FileManagerController::class, 'moveDirectories'])->name('file_manager.move_directories');
                Route::middleware('can:file_manager_update')->post('file_manager/move_files', [FileManagerController::class, 'moveFiles'])->name('file_manager.move_files');
                Route::middleware('can:file_managers_read')->get('file_manager/export', [FileManagerController::class, 'exportZip'])->name('file_manager.export');

                // RMA
                Route::middleware('can:rmas_edit')->post('rmas/history/{id}', [RmaController::class, 'addHistory'])->name('rmas.add_history');
                Route::middleware('can:rmas_index')->get('rmas', [RmaController::class, 'index'])->name('rmas.index');
                Route::middleware('can:rmas_show')->get('rmas/{id}', [RmaController::class, 'show'])->name('rmas.show');
                Route::middleware('can:rmas_delete')->delete('rmas/{id}', [RmaController::class, 'destroy'])->name('rmas.destroy');

                Route::middleware('can:products_edit')->post('products/status', [ProductController::class, 'updateStatus'])->name('products.update_status');
                Route::middleware('can:products_delete')->delete('products/delete', [ProductController::class, 'destroyByIds'])->name('products.batch_delete');
                Route::middleware('can:products_index')->get('products', [ProductController::class, 'index'])->name('products.index');
                Route::middleware('can:products_create')->get('products/create', [ProductController::class, 'create'])->name('products.create');
                Route::middleware('can:products_create')->post('products', [ProductController::class, 'store'])->name('products.store');
                Route::middleware('can:products_show')->get('products/{id}/edit', [ProductController::class, 'edit'])->name('products.edit');
                Route::middleware('can:products_edit')->put('products/{id}', [ProductController::class, 'update'])->name('products.update');
                Route::middleware('can:products_delete')->delete('products/{id}', [ProductController::class, 'destroy'])->name('products.destroy');

                // 订单
                Route::middleware('can:orders_index')->get('orders', [OrderController::class, 'index'])->name('orders.index');
                Route::middleware('can:orders_export')->get('orders/export', [OrderController::class, 'export'])->name('orders.export');
                Route::middleware('can:orders_show')->get('orders/shipping', [OrderController::class, 'shipping'])->name('orders.shipping.get');
                Route::middleware('can:orders_show')->get('orders/{id}', [OrderController::class, 'show'])->name('orders.show');
                Route::middleware('can:orders_edit')->put('orders/{id}/status', [OrderController::class, 'updateStatus'])->name('orders.update_status');
                Route::middleware('can:orders_edit')->put('orders/{id}/shipments/{shipmentId}', [OrderController::class, 'updateShipment'])->name('orders.update_shipment');
                Route::middleware('can:orders_edit')->post('orders/{id}/shipments', [OrderController::class, 'updateShipment'])->name('orders.update_shipment');

                // 运费
                Route::middleware('can:shipping_index')->get('shipping', [ShippingController::class, 'index'])->name('shipping.index');
                Route::middleware('can:shipping_create')->get('shipping/create', [ShippingController::class, 'create'])->name('shipping.create');
                Route::middleware('can:shipping_create')->post('shipping', [ShippingController::class, 'store'])->name('shipping.store');
                Route::middleware('can:shipping_show')->get('shipping/{id}/edit', [ShippingController::class, 'edit'])->name('shipping.edit');
                Route::middleware('can:shipping_edit')->put('shipping/{id}', [ShippingController::class, 'update'])->name('shipping.update');
                Route::middleware('can:shipping_delete')->delete('shipping/{id}', [ShippingController::class, 'destroy'])->name('shipping.destroy');

                // 交易流水
                Route::middleware('can:shipping_index')->get('transactions', [TransactionController::class, 'index'])->name('transactions.index');

                // 子账号
                // 后台用户
                Route::middleware('can:users_index')->get('users', [UserController::class, 'index'])->name('users.index');
                Route::middleware('can:users_create')->get('users/create', [UserController::class, 'create'])->name('users.create');
                Route::middleware('can:users_show')->get('users/{id}/edit', [UserController::class, 'edit'])->name('users.edit');
                Route::middleware('can:users_create')->post('users', [UserController::class, 'store'])->name('users.store');
                Route::middleware('can:users_edit')->put('users/{id}', [UserController::class, 'update'])->name('users.update');
                Route::middleware('can:users_delete')->delete('users/{id}', [UserController::class, 'destroy'])->name('users.destroy');

                // 提现
                Route::middleware('can:withdraws_index')->get('withdraws', [WithdrawController::class, 'index'])->name('withdraws.index');
                Route::middleware('can:withdraws_create')->get('withdraws/create', [WithdrawController::class, 'create'])->name('withdraws.create');
                Route::middleware('can:withdraws_create')->post('withdraws', [WithdrawController::class, 'store'])->name('withdraws.store');
            });

    });
